package cn.stylefeng.guns.modular.recy.utils;

import cn.hutool.core.lang.Filter;
import org.springframework.stereotype.*;
import org.springframework.context.annotation.*;
import cn.stylefeng.guns.modular.system.service.*;
import cn.stylefeng.roses.core.util.*;
import cn.stylefeng.guns.modular.system.entity.*;
import java.io.*;
import cn.hutool.core.util.*;
import cn.hutool.core.map.*;
import cn.stylefeng.guns.core.common.constant.factory.*;
import org.apache.commons.lang3.*;
import java.util.*;

@Component
@DependsOn({ "springContextHolder" })
public class DictUtils
{
    private DictService dictService;
    
    public DictUtils() {
        this.dictService = (DictService)SpringContextHolder.getBean((Class)DictService.class);
    }
    
    public static DictUtils me() {
        return (DictUtils)SpringContextHolder.getBean("dictUtils");
    }
    
    public Dict getById(final Long dictId) {
        return (Dict)this.dictService.getById((Serializable)dictId);
    }
    
    public boolean eq(final String fullcode, final Long dictId) {
        final Long id = this.getId(fullcode);
        return ObjectUtil.equal(id, dictId);
    }
    
    public Long getId(final String fullcode) {
        final Dict dict = this.getDict(fullcode);
        if (dict == null) {
            return -1L;
        }
        return dict.getDictId();
    }
    
    public Long id(final String fullcode) {
        return this.getId(fullcode);
    }
    
    public Dict dict(final String fullcode) {
        return this.getDict(fullcode);
    }
    
    public Dict getDict(final String fullcode) {
        final Map<String, Dict> dictMap = this.dictService.dictMap();
        final Dict dict = dictMap.get(fullcode);
        return dict;
    }
    
    public void dictWrapper(final Map<String, Object> map, final String... dictIds) {
        for (final String dictId : dictIds) {
            final Long id = MapUtil.getLong((Map)map, dictId);
            final String dictName = ConstantFactory.me().getDictName(id);
            map.put(dictId, StringUtils.defaultIfBlank((CharSequence)dictName, (CharSequence)"--"));
        }
    }
    
    public Map<String, Dict> getByType(final String typeCode) {
        final Map<String, Dict> dictMap = this.dictService.dictMap();
        final Map<String, Dict> filter = MapUtil.filter(dictMap, new Filter<Map.Entry<String, Dict>>() {
            @Override
            public boolean accept(Map.Entry<String, Dict> stringDictEntry) {
                    final String key = stringDictEntry.getKey();
                    return key.startsWith(typeCode);
            }
        });
        final Map<String, Dict> typeMap = new HashMap<String, Dict>();
        for (final String key : filter.keySet()) {
            typeMap.put(key.split("::")[1], filter.get(key));
        }
        return typeMap;
    }
}
